const OSS = require('ali-oss');
const app = require('express')();
const ossConfig = require('./ossConfig');
const resJson = require('./resJson');
const Server = require('./utils/httpServer');
const request = require('request');

let client = new OSS(ossConfig);

async function list(res, key) {
    try {
        // 不带任何参数，默认最多返回1000个文件。
        let list = await client.list({
            'max-keys': 1000
        });

        if (key) {
            const keyList = list.objects.filter(item => item.name.match(key));
            res.end(resJson.returnSuccess(keyList));
            return;
        }
        res.end(resJson.returnSuccess(list.objects));
    } catch (e) {
        console.log(e);
    }
}

app.get('/api/getMusicList', (req, res) => {
    res.writeHead(200, {
        'Content-Type': 'text/html;charset=utf-8'
    });
    const searchKey = req.query.q || '';
    list(res, searchKey);
})

app.get('/api/krc/:keyword', function (req, res) {
    let keyword = req.params.keyword;
    getMusicHash(keyword, res)
})

let currentRes = {};
function cb(data) {
    currentRes.send({
        code: 200,
        data,
        msg: '成功'
    })
}

function getMusicHash(k, res) {
    currentRes = res;
    let keyword = encodeURIComponent(k);
    let host = 'songsearch.kugou.com'
    let path = `/song_search_v2?callback=getMusicInfo&keyword=${keyword}&page=1&pagesize=1&platform=WebFilter`
    Server.httpGet(host, {}, path, true)
        .then(function (body) {
            eval(body);
        })
        .catch(function (err) {
            res.send({
                code: 404,
                msg: '网络好像有点问题'
            })
            console.log(err)
        })
}

function getMusicInfo(data, res) {
    let hash = data.data.lists[0].FileHash;
    let host = 'wwwapi.kugou.com'
    let path = `https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=cb&hash=${hash}&album_id=982088&dfid=3dpkD71to1kF0dLNXN0mL2La&mid=617d6e96cbf8bd8d15dc3a2d27ada83f&platid=4&_=1581843272747`
    Server.httpGet(host, {}, path, true)
        .then(function (body) {
            eval(body);
        })
        .catch(function (err) {
            res.send({
                code: 404,
                msg: '网络好像有点问题'
            })
            console.log(err)
        })
}

// async function getMusicHash(k, res) {
//     let keyword = encodeURIComponent(k);
//     let host = 'mobilecdn.kugou.com';
//     let path = `/api/v3/search/song?format=json&keyword=${keyword}&page=1&pagesize=1&showtype=1`;
//     // false:http请求  true:https请求
//     const body = await Server.httpGet(host, {}, path, false);
//     const data = JSON.parse(body).data.info[0];
//     const hash = data ? data.hash : '';
//     getKrc(hash, res);
// }

// function getKrc(hash, res) {
//     let host = 'm.kugou.com'
//     let path = `/app/i/krc.php?cmd=100&hash=${hash}&timelength=3012000`
//     // let host = 'wwwapi.kugou.com'
//     // let path = `/yy/index.php?r=play/getdata&callback=cb&hash=${hash}`
//     // false:http请求  true:https请求
//     Server.httpGet(host, {}, path, false)
//         .then(function (body) {
//             res.send({
//                 code: 200,
//                 data: body,
//                 msg: ''
//             })
//         })
//         .catch(function (err) {
//             res.send({
//                 code: 404,
//                 msg: '网络好像有点问题'
//             })
//             console.log(err)
//         })
// }

app.listen('3000', (err) => {
    if (err) console.log(err);
})